<template>
  <div>
    <div class="addPos">
      <el-input
          class="addPosInput"
          size="small"
          placeholder="添加职位.."
          prefix-icon="el-icon-plus"
          @keydown.enter.native="addPosition"
          v-model="pos.name">
      </el-input>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="addPosition">添加</el-button>
    </div>
    <div>
      <el-table
          border
          stripe
          :data="positions"
          class="posTable"
          size="small"
          @selection-change="handleSelectionChange"
          style="width: 70%">
        <el-table-column
            type="selection"
            width="45">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建日期">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showUpdateEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px ; display: flex">
        <el-button @click="deleteMore" small type="danger" :disabled="this.multipleSelection.length == 0">批量删除</el-button>
        <el-button @click="noDeleteMore">取消选择</el-button>
      </div>
    </div>
    <el-dialog
        title="编辑职位"
        :visible.sync="dialogVisible"
        width="30%"
    >
      <span class="dialogSpan">
        <el-tag type="success">职位</el-tag>
        <el-input size="small" v-model="updatePos.name" class="updatePosInput"></el-input>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="noUpdate">取 消</el-button>
        <el-button type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "PosMana",
  data() {
    return {
      pos: {
        name
      },
      positions: [],
      dialogVisible: false,
      updatePos: {
        name
      },
      beforeName: '',
      multipleSelection: []
    }
  },
  mounted() {
    this.initPositions();
  },
  methods: {
    noDeleteMore(){
      this.multipleSelection = [];
      this.initPositions();
    },
    deleteMore(){
      this.$confirm('此操作将删除[ ' + this.multipleSelection.length + ' ]条职位数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids = ids+'ids=' + item.id + '&'
        })
        this.deleteRequest('/api/system/basic/pos/' + ids).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    noUpdate() {
      this.dialogVisible = false;
      this.initPositions();
    },
    doUpdate() {
      this.putRequest('/api/system/basic/pos/', this.updatePos).then(resp => {
        if (resp) {
          this.initPositions();
          this.dialogVisible = false;
        }
      })
    },
    addPosition() {
      if (this.pos.name) {
        this.postRequest('/api/system/basic/pos/', this.pos).then(resp => {
          if (resp) {
            this.initPositions();
            this.pos.name = '';
          }
        })
      } else {
        this.$message.error('职位名称不能为空！');
      }
    },

    showUpdateEdit(index, date) {
      this.dialogVisible = true;
      this.updatePos = date;
      this.beforeName = date;
      // this.updatePos.createDate='';

    },
    handleDelete(index, date) {
      this.$confirm('此操作将删除[ ' + date.name + ' ], 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/api/system/basic/pos/' + date.id).then(resp => {
          if (resp) {
            this.initPositions();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    initPositions() {
      this.getRequest('/api/system/basic/pos/').then(resp => {
        if (resp) {
          this.positions = resp;
        }
      })
    }
  }

}
</script>

<style scoped>
.addPos {
  display: flex;
}

.addPosInput {
  width: 300px;
  margin-right: 8px;
}

.posTable {
  margin-top: 8px;
}

.updatePosInput {
  width: 160px;
  margin-left: 8px;
}

.dialogSpan {
  display: flex;
}
</style>